<?php
include_once("dbconnect/db.php");
Class Category
{
	var $errorMessage;
	
	var $categoryId;
	
	var $resultList = "";
	
	var $nowPageNum = 1;
	
	var $categoryName;
	
	var $totalLineNum = 0;
	
	var $fistLineNum = 0;
	
	var $lastLineNum = 0;
	
	var $orderType = "1";
	
	var $pageLinkHtml = "";
	
	// 毎Eージのレコード数
	var $RECORD_NUMBER_PER_PAGE = 5;
	
	function mytask()
	{
		// 初期化の場合
		if(isset($_POST["act"]) == false)
		{
			$this->categoryId = $_GET["categoryId"];
			$this->getCategoryName();
			$this->getResultList($this->nowPageNum);
			$this->errorMessage = "";
		}
		else if($_POST["act"] == "submit")
		{
			$this->initForm();
			$this->categoryId = $_GET["categoryId"];
			$this->getCategoryName();
			$this->getResultList($this->nowPageNum);
		}
	}
	
	function initForm()
	{
		$this->nowPageNum = $_POST["nowPage"];
		$this->orderType = $_POST["orderType"];
	}
	
	function getCategoryName()
	{
		$sql = "select `category_name` from `category_mst` where `category_id` =";
		$sql = $sql.$this->categoryId;
		$result = mysql_query($sql);		
		$data = mysql_fetch_row($result);		
		$this->categoryName = $data[0];
	}
	
	function getSelectSql()
	{
		$sql = "select `entrylist`.`title`,`videolist`.`cid`,`videolist`.`video_id`,`videolist`.`time`,`videolist`.`up_date`,`videolist`.`view_cnt`,`videolist`.`rating_avg` ";
		$sql = $sql."from `entrylist`,`videolist`,`urllist` ";
		$sql = $sql."where `urllist`.`category_id` =  ";
		$sql = $sql.$this->categoryId." ";
		$sql = $sql."and `videolist`.`site_id` = 1 ";
		$sql = $sql."and `videolist`.`open_flag` = 1 ";
		$sql = $sql."and `urllist`.`open_flag` = 1 ";
		$sql = $sql."and `entrylist`.`open_flag` = 1 ";
		$sql = $sql."and `videolist`.`cid` is not null ";
		$sql = $sql."and `urllist`.`urllist_id` = `entrylist`.`urllist_id` ";
		$sql = $sql."and `entrylist`.`entry_id` = `videolist`.`entry_id` ";
		return $sql;
	}
	
	function getAdvSql()
	{
		$sql = "select `adspace_id`,`ad_name`,`tag_contents` from `adspace_mst` where `type` = 2 and `open_flag` = 1 order by `adspace_id`";
		 
		return $sql;
	}
	
	function getOrderSql()
	{
		$sql = "";
		
		// 新着
		if($this->orderType == "1")
		{
			$sql = " order by up_date desc ";
		}
		// 人気
		else if ($this->orderType == "2")
		{
			$sql = " order by rating_avg desc ";
		}
		
		return $sql;
	}	
	
	function getResultList($pageNumb)
	{
		$fromRow = ($pageNumb-1) * $this->RECORD_NUMBER_PER_PAGE;
		$sql = $this->getSelectSql();
		$sql = $sql.$this->getOrderSql();
		$sql = $sql."limit ".$fromRow.",".$this->RECORD_NUMBER_PER_PAGE;
		
		$totalSql = $this->getSelectSql();
		
		$adSql = $this->getAdvSql();
		$resultAd = mysql_query($adSql);
		$rowsAd = mysql_num_rows($resultAd);
		
		while($row=mysql_fetch_array($resultAd)) 
		{
       		$dataAd[] = $row;
   		}

		
		$resultL = mysql_query($sql);
		$rowsL = mysql_num_rows($resultL);
		$resultT = mysql_query($totalSql);
		$rowsT = mysql_num_rows($resultT);
		
		if($rowsL > 0)
		{
			$fromRowNumber = $fromRow + 1;
			$toRowNumber  = $fromRowNumber + $rowsL -1;
			$this->fistLineNum = $fromRowNumber;
			$this->lastLineNum = $toRowNumber;
			$this->totalLineNum = $rowsT;
			$this->getPageLinkHtml($rowsT);
			
			for ($i=0;$i<$rowsL;$i++)
			{
				for($j=0;$j<$rowsAd;$j++)
				{					
					// 広告が存在の場合
					if($dataAd[$j]["adspace_id"] == $i + 1)
					{
						$this->resultList = $this->resultList."<hr color=#3399FF>".$dataAd[$j]["ad_name"]."<font size=1>";
						$this->resultList = $this->resultList.$dataAd[$j]["tag_contents"]."<br clear=all></font>";
					}					
				}
				
				$data = mysql_fetch_array($resultL);
					
				$this->resultList = $this->resultList."<hr color=#3399FF><a href=result.php?categoryId=".$this->categoryId."&videoId=".$data["video_id"]."&videoName=".urlencode($data["title"]).">".$data["title"]."</a><br><font size=1>";
				$this->resultList = $this->resultList."<a href=result.php?categoryId=".$this->categoryId."&videoId=".$data["video_id"]."&videoName=".urlencode($data["title"])."><img src=http://img.mytube.to/ir/70/http://img.youtube.com/vi/".$data["cid"]."/default.jpg align=left></a>";
				$this->resultList = $this->resultList."時間：".$data["time"]."秒<br>";
				$this->resultList = $this->resultList."登録：".$data["up_date"]."<br>";
				$this->resultList = $this->resultList."閲覧：".$data["view_cnt"]."回<br>";
				$this->resultList = $this->resultList."評価：".round($data["rating_avg"],0)."/5点<br>";
				$this->resultList = $this->resultList."<br clear=all></font>";				
			}
		}
		// データがない場合
		else
		{
			
		}
	}

	function getPageLinkHtml($totalRowNum)
	{
		$j = 0;
		$pageNum = 0;
		$firstPage = $this->nowPageNum;
		for ($i=$this->nowPageNum-1;$i < $totalRowNum/$this->RECORD_NUMBER_PER_PAGE;$i++)
		{
			$pageNum = $i+1;
			$j = $j + 1;

			$this->pageLinkHtml = $this->pageLinkHtml."<a href=\"javascript:document.getElementById('nowPage').value = ".$pageNum.";do_search();\">".$pageNum."|";														
			if ($j == 10)
			{
				break;
			}
		}
		
		if ($firstPage > 1)
		{
			$prePage = $pageNum - 20 + 1;
			if ($prePage < 0)
			{
				$this->pageLinkHtml = "<a href=\"javascript:document.getElementById('nowPage').value = "."1".";do_search();\">"."先へ".$this->pageLinkHtml;
			}
			else
			{
				$this->pageLinkHtml = "<a href=\"javascript:document.getElementById('nowPage').value = ".$prePage.";do_search();\">"."先へ".$this->pageLinkHtml;				
			}
		}
		if ($pageNum < $totalRowNum/$this->RECORD_NUMBER_PER_PAGE)
		{
			$nextPage = $pageNum + 1;
			$this->pageLinkHtml = $this->pageLinkHtml."<a href=\"javascript:document.getElementById('nowPage').value = ".$nextPage .";do_search();\">"."次へ";
		}			
	}
}
?>